package cn.tedu.rebbimq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.Principal;
import java.util.concurrent.TimeoutException;

public class Producer {
    private static final Logger log = LoggerFactory.getLogger(Principal.class);
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.连接 - 连接 , 通道
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        f.setVirtualHost("/ryan");  //连接虚拟主机;
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        /*2.定义队列
        * 告诉服务器我们要使用的队列是哪个队列
        * 告诉已经存在直接使用
        * 如果不存在,mq服务器会新建队列*/
        c.queueDeclare(
                "helloword",
                false,
                false,
                false,
                null
        );

        //3.发送消息
        c.basicPublish(
                "",
                "helloword",
                null,
                "helloworld".getBytes()
        );
        System.out.println("发送信息");
        //4.断开连接
        c.close();
        con.close();
    }
}
